public class Solution {

    public static boolean VerifySquenceOfBST(int[] sequence) {

        int length = sequence.length;

        if(length==1)
            return true;
        if(length<1)
            return false;

        int count_left=0,count_right=0;

        int m=0, n=0;

        int middle = sequence[length-1];

        boolean rightflag=false;


        for(int i=0;i<length-1;i++){
            if(sequence[i]<middle){
                count_left++;
                if (rightflag)
                    return false;
            }
            else{
                rightflag=true;
                count_right++;
            }

        }

        int[] left_a = new int[count_left];
        int[] right_a = new int[count_right];

        boolean left_r=true,right_r=true;

        if(count_left>=1)
            left_r = VerifySquenceOfBST(left_a);
        if(count_right>=1)
            right_r = VerifySquenceOfBST(right_a);

        return left_r && right_r;
    }
    public static void main(String args[]){
        int[] sequence = {4,8,6,12,16,14,10};
        System.out.println(VerifySquenceOfBST(sequence));
    }
}